<?php
	class OfferTable{
		
		public static function render($offers){
			$sortedOffers = self::prepareForOffers($offers);
			$bg = false;
			$out .= '';
			foreach ($sortedOffers as $offer){
				$out .= self::showOfferPerRow($offer, $bg);
				$bg = !$bg;
			}
			return $out;			
		}
		
		private static function prepareForOffers($offers){
			$sortedOffers = array();
			foreach ($offers as $offer){
				if(null != $offer['size']){
					$sortedOffers[$offer['id']]['creatives'][$offer['creative_url']] = Util::getCreativeSize($offer['size']);
				}
				if(null != $offer['landing_url']){
					$sortedOffers[$offer['id']]['landing_urls'][$offer['landing_id']] = $offer['landing_url'];
				}
				if(null != $offer['campaign_name']){
					$sortedOffers[$offer['id']]['campaign_names'][$offer['campaign_id']] = $offer['campaign_name'];
				}
				$sortedOffers[$offer['id']]['creatives_ids'][$offer['creative_id']] = $offer['creative_id'];
				$sortedOffers[$offer['id']]['id'] = $offer['id'];
				$sortedOffers[$offer['id']]['availability'] = Util::getOfferAvailability($offer['availability']);
				$sortedOffers[$offer['id']]['type'] = $offer['type'];
				$sortedOffers[$offer['id']]['price'] = $offer['price'];
				$sortedOffers[$offer['id']]['offer_name'] = $offer['offer_name'];
				$sortedOffers[$offer['id']]['status'] = Util::getOfferStatus($offer['status']);
			}
			return $sortedOffers;
		}
		
		private static function showOfferPerRow($offer, $bg){
			$out = '<tr';
			if($bg)
				$out .= ' class="bg"';
			$out .= '>';
			
			$out .= '<td>' . $offer['offer_name']  . '</td>';
			$out .= '<td>' . $offer['availability'] . '</td>';
			$out .= '<td>' . $offer['type'] . '</td>';
			$out .= '<td>' . $offer['price'] . '</td>';
			$out .= '<td>' . $offer['status'] . '</td>';
			$out .= self::showOfferCreatives($offer['creatives'], $offer['id']);
			$out .= self::showOfferLandingUrls($offer['landing_urls'], $offer['id']);
			$out .= self::showOfferCampaign($offer['campaign_names'], $offer['id']);
			$out .= self::showOfferAction($offer);
			$out .= '</tr>';
			return $out;
		}
		
		private static function showOfferCreatives($creatives, $offerId){
			$out = '<td id="gallery">';
			if(null !== $creatives){
				foreach ($creatives as $url=>$description){
					$out .= '<a href="' . $url . '"  target="_blank">' . $description . '</a><br/>';
				}
			}
			$out .= '<a href="' . url('offer/creatives', array('offer_id'=>$offerId)) . '">Setup</a>';
			$out .= '</td>';
			return $out;
		}
		
		private static function showOfferLandingUrls($landingUrls, $offerId){
			$out = '<td>';
			if(null != $landingUrls){
				foreach ($landingUrls as $url){
					$out .= '<a href="' . $url . '"  target="_blank">' . $url . '</a><br/>';
				}
			}
			$out .= '<a href="' . url('offer/landingurls', array('offer_id'=>$offerId)) . '">Setup</a>';
			$out .= '</td>';
			return $out;
		}
		
		private static function showOfferCampaign($campaigns, $offerId){
			$out = '<td>';
			if(null != $campaigns){
				foreach ($campaigns as $campaign){
					$out .= "$campaign<br/>";
				}
			}
			$out .= '<a href="' . url('offer/campaigns', array('offer_id'=>$offerId)) . '">Setup</a>';
			$out .= '</td>';
			return $out;
		}
		
		private static function showOfferAction($offer){
			$out = '<td class="action">';
			$out .= '<a href="' . url('offer/update', array('id'=>$offer['id'])) . '">Edit</a>';
			if('Active' != $offer['status'] && count($offer['creatives']) >= 1 && count($offer['landing_urls']) >=1 && 'Deleted' != $offer['status']){
				$out .= '<a href="' . url('offer/active', array('id'=>$offer['id'])) . '">Active</a>';
			}
			if(null === $offer['campaign_names'] && 'Active' == $offer['status']){
				$out .= '<a href="' . url('offer/pause', array('id'=>$offer['id'])) . '">Pause</a>';
				
			}
			if(null === $offer['campaign_names'] && 'Active' != $offer['status'] && 'Deleted' != $offer['status'] ){
				$out .= '<a href="' . url('offer/delete', array('id'=>$offer['id'])) . '">Delete</a>';
			}
			
			$out .= '<a href="' . url('offer/clone', array('id'=>$offer['id'])) . '">Clone</a>';
			$out .= '</td>';
			return $out;
		}
		
	}